/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.dao;

import com.home.common.Constants;
import com.home.common.LevelSubject;
import com.home.common.utils.HibernateUtil;
import com.home.mapping.Agent;
import com.home.mapping.Subject;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author vi
 */
public class SubjectDao extends BaseDao{
    public List<Subject> findAllSubjects() {
        List<Subject> list = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Query query = session.createSQLQuery("SELECT * FROM subject ORDER BY id_subject ASC").addEntity(Subject.class);
            
            if (query != null) {
                list = query.list();
            }
        } catch (HibernateException he) {
            he.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }
    public List<Subject> findByLevel(int level) {
        List<Subject> list = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Query query = session.createSQLQuery("SELECT * FROM subject WHERE subject.level = :level ORDER BY id_subject ASC")
                    .addEntity(Subject.class)
                    .setParameter("level", level);
            
            if (query != null) {
                list = query.list();
            }
        } catch (HibernateException he) {
            he.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }
    
    
}
